//
//  KZRecordEvaluationTool.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2019/1/3.
//  Copyright © 2019 zhukai. All rights reserved.
//

#import "KZRecordEvaluationTool.h"

#import "KYTestEngine.h"

@implementation KZRecordEvaluationTool
{
    NSString *_recordResult;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        _config = [[KYTestConfig alloc] init];
        _config.coreType = KYTestType_Sentence;
        _config.phonemeOption = KYPhonemeOption_KK;
        _config.audioType = @"wav";
        _config.sampleRate = 16000;
        _config.sampleBytes = 2;
    }
    return self;
}

/**
 识别单词
 
 @param wordsCallback 单词回调
 */
- (void)startRecordWithWordsCallback:(void (^)(NSString *, NSArray *))wordsCallback {
    
    _config.coreType = KYTestType_ASR;
    _config.soundIntensityEnable = false;

    [[KYTestEngine sharedInstance] startEngineWithTestConfig:_config result:^(NSString *recordResult) {
        NSLog(@"%@", recordResult);
        NSError *error;
        NSData *data = [recordResult dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves  error:&error];
        if (error) {
            if (wordsCallback) {
                wordsCallback(nil, nil);
            }
        } else {
            NSDictionary *resultDic = dic[@"result"];
            NSString *recognition = resultDic[@"recognition"];
            NSMutableArray *temp = [NSMutableArray new];
            NSArray *details = resultDic[@"details"];
            for (NSDictionary *subdic in details) {
                [temp addObject:subdic[@"word"]];
            }
            if (wordsCallback) {
                wordsCallback(recognition, temp.copy);
            }
        }
    }];
}

- (void)startRecordWithWordsCallback:(void (^)(NSString *, NSArray *))wordsCallback soundIntensityCallback:(void (^)(CGFloat))soundIntensityCallback {
    
    _config.coreType = KYTestType_ASR;
    _config.soundIntensityEnable = true;
    
    [[KYTestEngine sharedInstance] startEngineWithTestConfig:_config result:^(NSString *recordResult) {
        
        NSError *error;
        NSData *data = [recordResult dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
        if (error) {
            if (wordsCallback) {
                wordsCallback(nil, nil);
            }
            if (soundIntensityCallback) {
                soundIntensityCallback(0.0);
            }
        }

        // 包含音量回调
        if ([recordResult rangeOfString:@"sound_intensity"].length > 0) {
            CGFloat soundIntensity = [dic[@"sound_intensity"] floatValue];
            if (soundIntensityCallback) {
                soundIntensityCallback(soundIntensity);
            }
        } else {
            NSDictionary *resultDic = dic[@"result"];
            NSString *recognition = resultDic[@"recognition"];
            NSMutableArray *temp = [NSMutableArray new];
            NSArray *details = resultDic[@"details"];
            for (NSDictionary *subdic in details) {
                [temp addObject:subdic[@"word"]];
            }
            if (wordsCallback) {
                wordsCallback(recognition, temp.copy);
            }
        }
    }];
}
- (void)endRecord {
    
    [[KYTestEngine sharedInstance] stopEngine];
}


@end
